package com.ojoin.ojoin.eb_spider.service.catchdata;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.domain.BankCardCheck;
import com.ojoin.ojoin.eb_spider.service.impl.SLDataCatchBaseServiceImpl;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.NumberUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;

/**
 * @author Administrator url-->
 *         http://order.shop.jd.com/order/sSopUp_newYiShipList.action 1.
 *         
 *         
 */

//@Scope("prototype")
@Service
public class SupplierJDDataCatchServiceImpl extends SLDataCatchBaseServiceImpl {
	
	
	private static Logger log = LoggerFactory
			.getLogger(SupplierJDDataCatchServiceImpl.class);

	@Override
	public boolean LoginByUserInfo(String baseUrl, String username,
			String password) throws Exception {
		if (isCacheLogin()) {
			return true;
		}

		int loginCount = 0;
		// 开始登陆

		// 开始登陆 最多登陆三次（验证码错误情况）
		// 登陆返回信息
		while (loginCount < verifycodeRetryCount) {

			loginCount ++;
			
			this.getWebDriver().get(baseUrl);

			this.getWebDriver().sendKeys("id", "loginname", username);
			this.getWebDriver().sendKeys("id", "nloginpwd", password);

			boolean isCome = this.getWebDriver().findElement("id","o-authcode").isDisplayed();

			if (isCome) {
				verifycode();
				// 记录验证码请求次数
				loginCount++;
			}
  
			this.getWebDriver().click("id", "loginsubmit");
			  
			this.getWebDriver().get("http://vcp.jd.com/index/initIndexPage");
			
			 
			boolean loginFlag = this.getWebDriver().isLogin("http://vcp.jd.com/index/initIndexPage");

			if (loginFlag) {
				return true;
			}

		}

		return false;

	}


	public void verifycode() throws Exception {
		LogUtils.info(log,  this.getAssistCatchData().getStartMess().getPlatformUser().getPlatformName() + " 登陆界面包含验证码.");
		WebElement verifyE = this.getWebDriver().findElement("css","#o-authcode img");

		if (null != verifyE) {

			String code = this.getWebDriver().getVerifyCode(verifyE);
			LogUtils.info(log,  this.getAssistCatchData().getStartMess().getPlatformUser().getPlatformName()
					+ "verifyCode->" + code);

			WebElement verifycode = this.getWebDriver().findElement("id","authcode");

			this.getWebDriver().clearByJS(verifycode);
			// verifycode.clear();
			verifycode.sendKeys(code);

		}

	}

	@Override
	public String catchSupplierName() throws BusinessException {
		// 店铺信息url TODO
		return null;
	}

	@Override
	public BankCardCheck catchSupplierAccount() throws BusinessException {
		// 店铺信息url TODO
		   BankCardCheck bcc = new BankCardCheck();
		   return bcc;
	}

	@Override
	public boolean getUnpayAccountData() throws BusinessException {

		List<TbSlSupplyPayment> ListBefore = new ArrayList<TbSlSupplyPayment>();

		getUnBalanceAccountDataBefore(ListBefore);

		 this.getAssistCatchData().getCata().setPayupdate(ListBefore);

		 this.getAssistCatchData().getCata().setSettlement(ListBefore);

		LogUtils.info(log, "getUnpayAccountData()-->执行完毕:");

		return true;
	}

	public void getUnBalanceAccountDataBefore(List<TbSlSupplyPayment> slPList)
			throws BusinessException {
		// 未付款结算单

		String currentAccountUrl = "http://vcp.jd.com/index/initIndexPage";

		this.getWebDriver().get(currentAccountUrl);// 对账单

		String balanceAccount = this.getWebDriver()
				.getText(
						"css",
						"body > div.main > div.slide > div:nth-child(2) > div.modcon > div:nth-child(2)");
		
		if(!CommonUtils.isEmpty(balanceAccount)){
			balanceAccount = balanceAccount.split("元")[0];
		}

		String time = TimeUtils.getCurDate();

		TbSlSupplyPayment ordervo = new TbSlSupplyPayment();
		ordervo.setPlatformAccountId( this.getAssistCatchData().getStartMess().getPlatformUser().getId());
		ordervo.setSettlementId(time);
		ordervo.setCreateDate(time);
		ordervo.setSettlementState(TbSlSupplyPayment.WAITPAY);
		long longamount = NumberUtils.getLongAmount(balanceAccount);
		ordervo.setAmount(longamount);

		slPList.add(ordervo);

		LogUtils.info(log, "JDThird 抓取往期待结算数据：balanceOrderId->" + time + "\n"
				+ "balanceStartTime->" + time + "\n" + "balanceAccount->"
				+ balanceAccount + "\n");

	}

	@Override
	public boolean getInstoreOrderData() throws BusinessException {
		// TODO Auto-generated method stub
		return true;

	}

	@Override
	public boolean getPayAccountData() throws BusinessException {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean getOutstoreOrderData() throws BusinessException {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public BankCardCheck isSupplyAccountCorrect() throws BusinessException {
		BankCardCheck bcc = new BankCardCheck();
		bcc.setCorrect(true);
		
		return bcc;
	}
}
